package com.qf.day03_checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SecondActivity extends Activity {
	
	private CheckBox cbEat,cbSleep,cbHit,cbAll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        cbEat = (CheckBox) findViewById(R.id.cb_eat);
        cbSleep = (CheckBox) findViewById(R.id.cb_sleep);
        cbHit = (CheckBox) findViewById(R.id.cb_hit);
        cbAll = (CheckBox) findViewById(R.id.cb_all);
        
        //获取监听对象
        MyListner myListner = new MyListner();
        
        //设置checkBox状态是否改变的监听
        cbEat.setOnCheckedChangeListener(myListner);
        cbSleep.setOnCheckedChangeListener(myListner);
        cbHit.setOnCheckedChangeListener(myListner);
//      XXXX  cbAll.setOnCheckedChangeListener(myListner);XXXXX
        
        //做全选  用点击事件
        cbAll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CheckBox cb = (CheckBox) v;
				boolean flag = cb.isChecked();
				
				if(flag){//ture
					cbEat.setChecked(true);
					cbSleep.setChecked(true);
					cbHit.setChecked(true);
				}else{
					cbEat.setChecked(false);
					cbSleep.setChecked(false);
					cbHit.setChecked(false);
				}
			}
		});
        
        
    }
    
    public class MyListner implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			
			if(cbEat.isChecked()&&cbSleep.isChecked()&&cbHit.isChecked()){
				cbAll.setChecked(true);
			}else{
				cbAll.setChecked(false);
			}
			
		}
    	
    }
    

//    
//    public class MyListner implements OnCheckedChangeListener{
//
//    	/**
//    	 * 参数1:选择的控件
//    	 * 参数2:选择控件 的状态
//    	 */
//		@Override
//		public void onCheckedChanged(CompoundButton buttonView,
//				boolean isChecked) {
//			// TODO Auto-generated method stub
//			
//			switch (buttonView.getId()) {
//			case R.id.cb_eat:
//				if(isChecked){//ture
//					Toast.makeText(SecondActivity.this, "选择了吃饭", Toast.LENGTH_SHORT).show();
//				}else{
//					Toast.makeText(SecondActivity.this, "==没有选择了吃饭==", Toast.LENGTH_SHORT).show();
//				}
//				
//				break;
//			case R.id.cb_sleep:
//				if(isChecked){//ture
//					Toast.makeText(SecondActivity.this, "选择了睡觉", Toast.LENGTH_SHORT).show();
//				}else{
//					Toast.makeText(SecondActivity.this, "==没有选择了睡觉==", Toast.LENGTH_SHORT).show();
//				}
//				break;
//			case R.id.cb_hit:
//				if(isChecked){//ture
//					Toast.makeText(SecondActivity.this, "选择了打豆豆", Toast.LENGTH_SHORT).show();
//				}else{
//					Toast.makeText(SecondActivity.this, "==没有打豆豆==", Toast.LENGTH_SHORT).show();
//				}
//				break;
////			case R.id.cb_all:
////				if(isChecked){//ture
////					cbEat.setChecked(true);
////					cbSleep.setChecked(true);
////					cbHit.setChecked(true);
////					
////				}else{
////					cbEat.setChecked(true);
////					cbSleep.setChecked(true);
////					cbHit.setChecked(true);
////				}
////				break;
//
//			default:
//				break;
//			}
//			
//		}
//    	
//    }
    

   
}
